
/*Do-file for "Treating abandoned mine drainage can protect streams cost effectively and benefit vulnerable communities." Run on Stata/SE Version 18.0.
The dataset "SystemlevelData" is at the treatment system level, with each row corresponding to a treatment system.*/
 
clear

use SystemlevelData

drop dph

*dropping systems with no flow
drop if iNoFlow==1

*25th, 50th, and 75th percentile for treatment effectiveness

foreach i in ph tmn tal tfe tss {  
	qui: gen d`i'=`i'1-`i'0
	qui: sum d`i', detail
	matrix `i'_het=r(p25),r(p50),r(p75)
}

matrix het=ph_het\tmn_het\tal_het\tfe_het\tss_het

*t-test for differences in means

foreach i in ph tmn tal tfe /*so4*/ tss {  
	qui: ttest `i'0 == `i'1
	matrix N1= r(N_1)
	matrix N2= r(N_2)
	matrix N3= .
	matrix N4= .
	matrix a1= r(mu_1)
	matrix a2= r(mu_2)
	matrix a4= r(p)
	matrix a5= r(mu_2)-r(mu_1)
	matrix `i'= a1,a2,a5,a4
}

*Creating matrix of statistics and providing row and column names
	matrix t1= (ph\tmn\tal\tfe\tss),het
	matrix colnames t1 = "Inflow Mean" "Outflow Mean" "Diff." "P-Value" "P25" "P50" "P75"
	matrix rownames t1 = "pH (log scale)" "Manganese (mg/L)" "Aluminum (mg/L)" "Iron (mg/L)" "Total Suspended Solids (mg/L)"

*Exporting table
	esttab matrix(t1, fmt(%12.2fc)) using "dstats_teffect.tex", tex nomtitles replace

			
